const Toast = require('../utils/toast.js')
const API = require('../utils/api.js')
/**
 * 封装HTTPget请求，
 * url:请求路径
 */
function getRequest(url) {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'Content-type': 'application/json',
        'Authorization': wx.getStorageSync('token')
      },
      success: function(response) {
        if (response.data.code === 200) {
          resolve(response.data)
        } else {
          console.log(response.msg)
        }
      },
      fail: function(err) {
        console.log(err.msg)
      }
    })
  })
}


/**
 * 封装HTTP post请求
 * url:请求路径
 * param:请求数据
 */
function postRequest(url, param) {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      method: 'POST',
      data: param,
      header: {
        'Content-type': 'application/json',
        'Authorization': wx.getStorageSync('token')
      },
      success: function(response) {
        if (response.data.code === 200) {
          resolve(response.data)
        } else {
          console.log(response.msg)
        }
      },
      fail: function(err) {
        console.log(response.msg)
      }

    })
  })
}

/**
 * 封装HTTP PUT请求
 * url:请求路径
 * param:请求数据
 */
function putRequest(url, param, loadding = true) {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      method: 'PUT',
      data: param,
      header: {
        'Content-type': 'application/json',
        'Authorization': wx.getStorageSync('token')
      },
      success: function(response) {
        resolve()
      },
      fail: function(err) {
        console.log(response.msg)
      }

    })
  })
}

/**
 * 封装HTTP del请求
 * url:请求路径
 * param:请求数据
 */
function delRequest(url, param) {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      method: 'DELETE',
      data: param,
      header: {
        'Content-type': 'application/json',
        'Authorization': wx.getStorageSync('token')
      },
      success: function(response) {
        if (response.data.code === 200) {
          resolve(response.data)
        } else {
          console.log(response.msg)
        }
      },
      fail: function(err) {
        console.log(response.msg)
      }

    })
  })
}
/**
 * 保存formId
 */
function saveFormId(formId) {
  return new Promise(function(resolve, reject) {
    let formIdData = {};
    formIdData.formId = formId;
    formIdData.openId = wx.getStorageSync('openId');
    wx.request({
      url: API.api.wxformids,
      method: 'POST',
      data: formIdData,
      header: {
        'Content-type': 'application/json',
        'Authorization': wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(`保存formId成功！！！`, res)
        resolve();
      }
    })
  })
}

module.exports = {
  getRequest: getRequest,
  postRequest: postRequest,
  saveFormId: saveFormId,
  delRequest: delRequest,
  putRequest: putRequest
}